package edu.dublbo.app.generator.controller;

import edu.dublbo.app.generator.entity.TBasicModel;
import edu.dublbo.app.generator.service.BasicModelService;
import edu.dublbo.app.generator.util.StrUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author DubLBo
 * @since 2020-09-05 19:14
 * i believe i can i do
 */
@Controller
public class ViewController {
    private static final Logger logger = LoggerFactory.getLogger(ViewController.class);

    @Resource
    private BasicModelService modelService;

    @RequestMapping("/")
    public String home(HttpServletRequest req) {
        req.setAttribute("routeUrl", "pages/model.html");
        req.setAttribute("pageTitle", "首页 - g-code-generator");
        return "pages/layout/index";
    }

    @RequestMapping("/model/detail/{id}")
    public ModelAndView showModelDetail(@PathVariable(value = "id") String modelId) {
        // 获取 model信息
        TBasicModel model = modelService.get(modelId);

        ModelAndView mav = new ModelAndView("pages/layout/index");

        mav.addObject("model", model);
        mav.addObject("routeUrl", "pages/model_detail_v3.html");
        mav.addObject("pageTitle",
                (StrUtils.isEmpty(model.getModelName()) ? "" : model.getModelName() + " | ")  + "模型明细");
        return mav;
    }

    @PostMapping("/project-config")
    public ModelAndView showProjectConfig(@RequestBody List<TBasicModel> models) {
        ModelAndView mav = new ModelAndView("pages/layout/index");

        mav.addObject("model", models);
        mav.addObject("routeUrl", "pages/project_config.html");

        return mav;
    }




//    public void setService(BasicModelService service) {
//        this.service = service;
//    }

    // 增加 模型
//    @PostMapping
//    public ResponseResult<TBasicModel> add(@RequestBody @Validated TBasicModel model) {
//        TBasicModel entity = service.add(model);
//        return ResponseResult.generateSuccessResult(entity);
//    }
//
//    // 删除 模型
//    @DeleteMapping(value = "/{id}")
//    public ResponseResult<BaseResponseData> delete(@PathVariable String id) {
//        if (StringUtils.isEmpty(id)) {
//            throw new OptErrorException(OptStatus.FAIL.getOptCode(), "请求参数为空");
//        }
//        TBasicModel entity = service.get(id);
//
//        if (entity == null) {
//            throw new OptErrorException(OptStatus.FAIL.getOptCode(), "要删除的对象不存在");
//        }
//        service.delete(entity);
//
//        return ResponseResult.generateSuccessResult();
//    }
//
//    // 查询所有存在的模型
//    @GetMapping("/s")
//    public Result<Map<String, Object>> list(@RequestParam(value = "modelName", required = false) String modelName) {
//        Map<String, Object> condition = new HashMap<>();
//        logger.info("modelName : {}", modelName);
//        condition.put("name", modelName == null ? null : modelName.trim());
//        condition.put("_order", "modify_time");
//        condition.put("_sort", "DESC");
////        logger.info("delete flag : {}", condition.get("deleteFlag") == null ? null : condition.get("deleteFlag").toString());
//        List<TBasicModel> all = service.list(condition);
//
//        Map<String, Object> records = new HashMap<>();
//        records.put("records", all);
//        return ResponseResult.generateSuccessResult(records);
//    }
//
//    @PutMapping(value = "/{id}")
//    public ResponseResult<TBasicModel> edit(@PathVariable String id, HttpServletRequest request) {
//
//        TBasicModel entity = service.get(id);
//        if (entity == null) {
//            throw new OptErrorException(OptStatus.FAIL.getOptCode(), "请求的对象不存在");
//        }
//
//        WebObjectUtils.assignObjectField(request, entity, "name", "remark", "tableName", "sortNo");
//
//        service.edit(entity);
//        return ResponseResult.generateSuccessResult(entity);
//    }
//
//    @GetMapping(value = "/{id}")
//    public ResponseResult<TBasicModel> detail(@PathVariable String id) {
//        TBasicModel model = service.get(id);
//        if (model == null) {
//            throw new OptErrorException(OptStatus.FAIL.getOptCode(), "要查看的记录不存在");
//        }
//        return ResponseResult.generateSuccessResult(model);
//
//    }
}
